window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		var curPage = 1;
		var pageSize = 10;
		var totalPage = 0;
		var keyword = "";
		var typ = 0;
		var state = -2; //(项目状态1-未开始，2-进行中，5-审核中，4-已完成)
		var provinceId = 1; //1(1-默认排序,2-时间排序)
		var fist=true;
		var cityId=0;
		U.fillHead();
		U.fillFooter();
		initPage();
		function initPage() { //初始化页面
			keyword = U.getParam("keyword");
			$(".name").val(keyword);
			U.loadOpt(false);
			getlist();
		}
		function initEvent() {
			$(".btn").unbind("click").click(function() {
				keyword = $(".name").val();
				getlist();
			});
			$(".classify>span").unbind("click").click(function() {
				if($(this).index()==0){
					return false;
				}
				cityId=$(this).attr("id")
				$(".classify>span").css("background-color", "white");
				$(".classify>span").css("color", "black");
				$(this).css("background-color", "#E13C44");
				$(this).css("color", "white");
				getlist();
			});
			$(".orderlist").unbind("click").click(function() {
				provinceId = $(this).attr("order");
				getlist();
			});
			$(".state>span").unbind("click").click(function() {
				state = $(this).attr("orderid");
				console.log(state);

				$(".state>span").css("background-color", "white");
				$(".state>span").css("color", "black");
				$(this).css("background-color", "#E13C44");
				$(this).css("color", "white");
				getlist();
			});
			$(".project_list>div").width((($(".project_list").width()) - 24 * 4 - 2 * 3) / 3);
			$(".project_list>div").unbind("click").click(function() {
				/*项目名称*/
				//console.log($(this).find(".p_name").attr("data-value"));
				/*时间节点*/
				//console.log($(this).find("img").attr("src"));
				/*已达百分比*/
				//console.log($(this).find("svg").attr("data-value"));
				/*已筹款*/
				//console.log($(this).find(".totalAmount").attr("data-value"));
				/*剩余时间*/
				//console.log($(this).find(".residualTime").attr("data-value"));
				/*图片*/
				//console.log($(this).find("img").attr("src"));
				/*描述*/
				//console.log($(this).find(".intro").attr("data-value"));
				/*window.open("special_details.html?id=" + $(this).attr("id"));*/

				window.open("special_details.html?id=" + $(this).attr("id"));
			});
		}

		function paging_handle(page, method) {
		
			//分页导航数据处理
			totalPage = page.totalPage;
			$("#paging .page").empty();
			var start_count = 0;
			try {
				if(curPage >= 10) {
					start_count = curPage - 5;
				}
			} catch(e) {}
			var lastsize = totalPage;
			lastsize = start_count + 10;
			
			if(lastsize>=totalPage){
				lastsize=totalPage;
			}
			for(var i = start_count; i < lastsize; i++) {
				if(i + 1 == curPage) {
					$("#paging .page").append("<div style='color:white;border-color:white;background-color:#DC3F44'>" + (i + 1) + "</div>");
				} else {
					$("#paging .page").append("<div>" + (i + 1) + "</div>");
				}
			}
			$(".Record>span:nth-child(1)").text("共" + page.totalItem + "条记录");
			$(".Record>span:nth-child(2)").text(curPage + "/" + page.totalPage);
			$("#paging .goto_page>input").val(curPage);
			//分页导航数据处理完毕
			//处理单击事件
			$(".next_page").unbind("click").click(function() {
				if(!page.nextPage) {
					alert("已经是最后一页了!")
					return false;
				}
				curPage++;
				method();
			});
			$(".up_page").unbind("click").click(function() {
				if(curPage == 1) {
					alert("已经是第一页了!");
					return false;
				}
				curPage--;
				method();
			});
			$("#paging .page>div").unbind("click").click(function() {
				curPage = $(this).text();
				method();
			});
			$("#paging .goto").unbind("click").click(function() {
				try {
					var index = $("#paging .goto_page>input").val();
					if(index <= totalPage && index > 0 && !/^[+-]?[1-9]?[0-9]*\.[0-9]*$/.test(index)) {
						curPage = $("#paging .goto_page>input").val();
						method();
					} else {
						alert("请输入正确的页码！");
					}
				} catch(e) {
					alert("请输入正确的页码！");
				}
			});
			//单机事件绑定完毕
		
		
		}
		//分页方法
		function getlist() {
			var params = {
				'title': keyword,
				curPage: curPage,
				pageSize: 10,
				name: keyword,
				typ: typ,
				cityId:cityId,
				state: state, //-2-全部，1-未开始，2-进行中，5-审核中，4-已完成
				provinceId: provinceId //1(1-默认排序,2-时间排序)
			};
			U.loadOpt(false);
			U.ajax("/crowd/specialList", params, function(json) {
				if(json.success) {
					$(".project_list").empty();
					$(".project_list").hide();

					$(json.models.specialList).each(function() {
						var percentage=this.progress.substring(0,this.progress.length-1);
						var day=this.residualTime.substring(0,this.residualTime.length-1);
						var p_state = "项目未开展>";
						if(this.state == 2) {
							p_state = "项目进行中>";
						}
						if(this.state == 3) {
							p_state = "项目已完成>";
						}
						var p_img = U.loadfail;
						if(U.getImg(this.images).length > 0) {
							p_img = U.getImg(this.images)[0];
						}
						var adv ="<script  src='js/jquery.progress.js'></script>";
						adv =adv+
							"<div id='" + this.id + "' >" +
								"<div class='p_name' data-value='"+this.title+"'>" + this.title + "</div>" +
								"<div class='p_img'><img src='" + p_img + "'></div>" +
								"<div class='crowd-center'>" +
									"<div class='progress'>" +
											"<svg class='container' data-value='"+percentage+"'></svg>" +
											"<div style='display: none' class='intro' data-value='"+this.intro+"' ></div>" +
											"<div style='display: none' class='gmtCreate' data-value="+U.getMyDate(this.gmtCreate)+" ></div>" +
									"</div>" +
								"</div>" +
								"<div class='crowd-bottom'>" +
									"<div class='crowd-bottom-item'>" +
										"<span class='sp1 progressValue' data-value='"+this.progress+"'>"+(this.state==3?"已完成":this.progress)+"</span>" +
										"<span class='sp3'>已达</span>" +
									"</div>" +
									"<div class='crowd-bottom-item'>" +
										"<span class='sp2 totalAmount' data-value='"+(this.totalAmount/100)+"'>"+(this.totalAmount/100)+"元</span>" +
										"<span class='sp3'>已筹款</span>" +
									"</div>" +
									"<div class='crowd-bottom-item'>" +
										"<span class='sp1 residualTime' data-value='"+this.residualTime+"'>"+(this.state==3?"已完成":this.residualTime)+"</span>" +
										"<span class='sp3'>剩余时间</span>" +
									"</div>" +
								"</div>" +
								"<div class='p_state' data-value='" + this.state + "'>" + p_state + "</div>" +
							"</div>";
						$(".project_list").append(adv);
					});

					var sr="<script> $('.container').each(function(){var progress = $(this).Progress({percent: $(this).attr('data-value'),width: 270,height:10,fontSize: 0});}); </script>"

					$(".project_list").append(sr);
					paging_handle(json.models.page, getlist); //分页调用方法
					U.loadOpt(false);
					$(".project_list").show(50, function() {
						initEvent();
					});
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}
	});
};